<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="手机号" prop="phoneNumber">
              <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="userName">
              <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="期望职位" prop="expectedPosition">
              <el-select v-model="queryParams.expectedPosition" placeholder="请选择期望职位" clearable >
                <el-option v-for="dict in app_position" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="求职状态" prop="jobStatus">
              <el-select v-model="queryParams.jobStatus" placeholder="请选择求职状态" clearable >
                <el-option v-for="dict in app_job_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="工作城市编码" prop="workCityCode">
              <el-input v-model="queryParams.workCityCode" placeholder="请输入工作城市编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker clearable
                v-model="queryParams.updateTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择更新时间"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:resume:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:resume:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:resume:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:resume:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="resumeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="简历ID" align="center" prop="resumeId" v-if="false" />
        <el-table-column label="用户ID" align="center" prop="userId" />
        <el-table-column label="手机号" align="center" prop="phoneNumber" />
        <el-table-column label="姓名" align="center" prop="userName" />
        <el-table-column label="头像" align="center" prop="avatarUrl" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.avatarUrl" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center" prop="gender">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.gender"/>
          </template>
        </el-table-column>
        <el-table-column label="出生日期" align="center" prop="birthDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="地区编码" align="center" prop="areaCode" />
        <el-table-column label="期望职位" align="center" prop="expectedPosition">
          <template #default="scope">
            <dict-tag :options="app_position" :value="scope.row.expectedPosition"/>
          </template>
        </el-table-column>
        <el-table-column label="期望薪资" align="center" prop="expectedSalary">
          <template #default="scope">
            <dict-tag :options="app_salary_range" :value="scope.row.expectedSalary"/>
          </template>
        </el-table-column>
        <el-table-column label="薪资备注说明" align="center" prop="salaryNote" />
        <el-table-column label="求职状态" align="center" prop="jobStatus">
          <template #default="scope">
            <dict-tag :options="app_job_status" :value="scope.row.jobStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="求职喜好" align="center" prop="jobPreferences" />
        <el-table-column label="工作经验" align="center" prop="workExperience">
          <template #default="scope">
            <dict-tag :options="app_work_experience" :value="scope.row.workExperience"/>
          </template>
        </el-table-column>
        <el-table-column label="学历" align="center" prop="education">
          <template #default="scope">
            <dict-tag :options="app_personal_education" :value="scope.row.education"/>
          </template>
        </el-table-column>
        <el-table-column label="资格证书" align="center" prop="certificates">
          <template #default="scope">
            <dict-tag :options="app_certificate_req" :value="scope.row.certificates ? scope.row.certificates.split(',') : []"/>
          </template>
        </el-table-column>
        <el-table-column label="工作城市编码" align="center" prop="workCityCode" />
        <el-table-column label="所属企业ID" align="center" prop="enterpriseId" />
        <el-table-column label="简历状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="app_resume_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="浏览量" align="center" prop="viewCount" />
        <el-table-column label="联系次数" align="center" prop="contactCount" />
        <el-table-column label="收藏数" align="center" prop="favoriteCount" />
        <el-table-column label="分享次数" align="center" prop="shareCount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:resume:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:resume:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改简历信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="resumeFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
                v-for="dict in sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地区编码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入地区编码" />
        </el-form-item>
        <el-form-item label="期望职位" prop="expectedPosition">
          <el-select v-model="form.expectedPosition" placeholder="请选择期望职位">
            <el-option
                v-for="dict in app_position"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="期望薪资" prop="expectedSalary">
          <el-select v-model="form.expectedSalary" placeholder="请选择期望薪资">
            <el-option
                v-for="dict in app_salary_range"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资备注说明" prop="salaryNote">
          <el-input v-model="form.salaryNote" placeholder="请输入薪资备注说明" />
        </el-form-item>
        <el-form-item label="求职状态" prop="jobStatus">
          <el-radio-group v-model="form.jobStatus">
            <el-radio
              v-for="dict in app_job_status"
              :key="dict.value"
              :value="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="求职喜好" prop="jobPreferences">
          <el-input v-model="form.jobPreferences" placeholder="请输入求职喜好" />
        </el-form-item>
        <el-form-item label="工作经验" prop="workExperience">
          <el-select v-model="form.workExperience" placeholder="请选择工作经验">
            <el-option
                v-for="dict in app_work_experience"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历">
            <el-option
                v-for="dict in app_personal_education"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资格证书" prop="certificates">
          <el-checkbox-group v-model="form.certificates">
            <el-checkbox
                v-for="dict in app_certificate_req"
                :key="dict.value"
                :label="dict.value">
                {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="工作城市编码" prop="workCityCode">
          <el-input v-model="form.workCityCode" placeholder="请输入工作城市编码" />
        </el-form-item>
        <el-form-item label="所属企业ID" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入所属企业ID" />
        </el-form-item>
        <el-form-item label="简历状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择简历状态">
            <el-option
                v-for="dict in app_resume_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Resume" lang="ts">
import { listResume, getResume, delResume, addResume, updateResume } from '@/api/system/resume';
import { ResumeVO, ResumeQuery, ResumeForm } from '@/api/system/resume/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { app_salary_range, app_work_experience, app_job_status, app_certificate_req, app_personal_education, sys_user_sex, app_resume_status, app_position } = toRefs<any>(proxy?.useDict('app_salary_range', 'app_work_experience', 'app_job_status', 'app_certificate_req', 'app_personal_education', 'sys_user_sex', 'app_resume_status', 'app_position'));

const resumeList = ref<ResumeVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const resumeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ResumeForm = {
  userId: undefined,
  phoneNumber: undefined,
  userName: undefined,
  avatar: undefined,
  gender: undefined,
  birthDate: undefined,
  areaCode: undefined,
  expectedPosition: undefined,
  expectedSalary: undefined,
  salaryNote: undefined,
  jobStatus: undefined,
  jobPreferences: undefined,
  workExperience: undefined,
  education: undefined,
  certificates: [],
  workCityCode: undefined,
  enterpriseId: undefined,
  status: undefined,
}
const data = reactive<PageData<ResumeForm, ResumeQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phoneNumber: undefined,
    userName: undefined,
    expectedPosition: undefined,
    jobStatus: undefined,
    workCityCode: undefined,
    updateTime: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询简历信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listResume(queryParams.value);
  resumeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  resumeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: ResumeVO[]) => {
  ids.value = selection.map(item => item.resumeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加简历信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: ResumeVO) => {
  reset();
  const _resumeId = row?.resumeId || ids.value[0]
  const res = await getResume(_resumeId);
  Object.assign(form.value, res.data);
  form.value.certificates = form.value.certificates.split(",");
  dialog.visible = true;
  dialog.title = "修改简历信息";
}

/** 提交按钮 */
const submitForm = () => {
  resumeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
        form.value.certificates = form.value.certificates.join(",");
      if (form.value.resumeId) {
        await updateResume(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addResume(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: ResumeVO) => {
  const _resumeIds = row?.resumeId || ids.value;
  await proxy?.$modal.confirm('是否确认删除简历信息编号为"' + _resumeIds + '"的数据项？').finally(() => loading.value = false);
  await delResume(_resumeIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/resume/export', {
    ...queryParams.value
  }, `resume_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
